package com.partsoft.dits.integrate;


import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Properties;


import com.partsoft.dits.ConfigurableBean;
import com.partsoft.dits.DataDescriptorManager;
import com.partsoft.dits.log.Log;

/**
 * 消息表入库描述符注册管理器
 */
public class ContextTableDataSavedDescriptorManager extends
		ConfigurableTableDataDescriptorManager<BaseTableDataEdgeDescriptor>
		implements ConfigurableBean, TableDataSavedDescriptorManager,
		TableDataDescriptorRegistry<BaseTableDataEdgeDescriptor>, 
		DataDescriptorManager<BaseTableDataEdgeDescriptor>,
		ApplicationContextAware, ApplicationListener<ApplicationEvent> {

	public Class<BaseTableDataEdgeDescriptor> getSupportClass() {
		return BaseTableDataEdgeDescriptor.class;
	}

	@Override
	protected void beforeRemoveDescriptor(
			BaseTableDataEdgeDescriptor descriptor) {
		
	}
	
	@Override
	protected void setupDescriptorIdentifier(BaseTableDataEdgeDescriptor descriptor, String id) {
		descriptor.setId(id);
	}

	@Override
	protected Properties beforeAddDescriptor(
			BaseTableDataEdgeDescriptor descriptor) {
		synchronized (this) {
			
			//保存脚本
			descriptor.setTransformScriptPath(resolveDescriptorScriptRelativeFileName(descriptor));
			try {
				saveTransformScriptContext(descriptor);
			} catch (IOException e) {
				throw new IllegalStateException(e.getMessage(), e);
			}
			
			Properties beanProps = new Properties();
			beanProps.setProperty("dataSourceName", descriptor.getDataSourceName());
			beanProps.setProperty("schemaName", descriptor.getSchemaName());
			beanProps.setProperty("tableName", descriptor.getTableName());
			beanProps.setProperty("keyFields", descriptor.getKeyFields());
			beanProps.setProperty("messageCode", descriptor.getMessageCode());
			beanProps.setProperty("transformScriptPath", descriptor.getTransformScriptPath());
			beanProps.setProperty("conversion", new Boolean(descriptor.isConversion()).toString());
			beanProps.setProperty("title", descriptor.getTitle());
			beanProps.setProperty("description", descriptor.getDescription());
			beanProps.setProperty("ordinal", Integer.valueOf(descriptor.getOrdinal()).toString());

			return beanProps;
		}
	}

	@Override
	protected void beforeLoadConfiguredDescriptor(
			BaseTableDataEdgeDescriptor descriptor) {
		try {
			descriptor.setTransformScript(loadTransformScriptContent(descriptor));
		} catch (IOException e) {
			if (descriptor.isConversion()) {
				String errMsg = String.format("加载(%s)的转换脚本失败: %s", descriptor.toString(), e.getMessage());
				Log.error(errMsg, e);
				throw new IllegalStateException(errMsg, e);
			} else {
				descriptor.setTransformScript("");
			}
		}
	}
	
	@Override
	protected String resolveDescriptorScriptRelativeFileName(BaseTableDataEdgeDescriptor descriptor) {
		return StringUtils.hasText(descriptor.getTransformScriptPath()) ? descriptor.getTransformScriptPath() : super.resolveDescriptorScriptRelativeFileName(descriptor);
	}
}
